package com.renwei.yunlong.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.renwei.yunlong.activity.me.RegisterAddressActivity;
import com.renwei.yunlong.bean.AddressJsonBean;
import com.renwei.yunlong.event.AddressLoadEvent;
import com.renwei.yunlong.utils.GetJsonDataUtil;

import org.json.JSONArray;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.service
 * Created by :  Benjamin.
 * Created time: 2017/11/10 14:56
 * Changed by :  Benjamin.
 * Changed time: 2017/11/10 14:56
 * Class description:
 */

public class AddressLoadService extends IntentService {
    private static String TAG = "AddressLoadService";


    public AddressLoadService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        /**
         * 注意：assets 目录下的Json文件仅供参考，实际使用可自行替换文件
         * 关键逻辑在于循环体
         *
         * */
        String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据
        ArrayList<AddressJsonBean> addressJsonBean = parseData(JsonData);//用Gson 转成实体
        RegisterAddressActivity.options1Items = addressJsonBean;
        for (AddressJsonBean addressBean : addressJsonBean) {//遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）
            for (AddressJsonBean.CityBean cityBean : addressBean.getCityList()) {
                String CityName = cityBean.getName() == null ? "" : cityBean.getName();
                CityList.add(CityName);//添加城市
                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (cityBean.getArea() == null || cityBean.getArea().size() == 0) {
                    City_AreaList.add("");
                } else {
                    //添加该城市所有地区数据
                    City_AreaList.addAll(cityBean.getArea());
                }
                Province_AreaList.add(City_AreaList);//添加该省所有城市数据
            }
            /**
             * 添加城市数据
             */
            RegisterAddressActivity.options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            RegisterAddressActivity.options3Items.add(Province_AreaList);
        }
        EventBus.getDefault().post(new AddressLoadEvent(AddressLoadEvent.loadSuccess));
    }


    public ArrayList<AddressJsonBean> parseData(String result) {//Gson 解析
        ArrayList<AddressJsonBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                AddressJsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), AddressJsonBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Eventbus发送地址信息加载错误消息
            EventBus.getDefault().post(new AddressLoadEvent(AddressLoadEvent.loadError));
        }
        return detail;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

